package com.telnetnz.ticketdesk.action.base;

import org.apache.log4j.Logger;

import com.rockbb.common.simpleframe.action.BasicAction;
import com.rockbb.common.simpleframe.config.SimpleConfig;
import com.rockbb.common.simpleframe.result.ActionResult;
import com.rockbb.common.sso.SessionService;
import com.rockbb.common.sso.acl.ACLUtils;
import com.rockbb.common.sso.session.Session;
import com.rockbb.common.sso.session.SessionConstants;
import com.rockbb.common.utilities.i18n.MessageResource;
import com.rockbb.common.utilities.i18n.MessageResourceFactory;
import com.telnetnz.ticketdesk.bean.User;
import com.telnetnz.ticketdesk.service.UserService;

public class BasicSessionAction extends BasicAction
{
	private static Logger logger = Logger.getLogger(BasicSessionAction.class);
	protected MessageResource langRes;
	protected int langId;

	public void prepare()
	{
		super.prepare();

		loadSession();

		User user = getSessionUser();

		if (user.getId() == SessionConstants.USER_ID_ANONYMOUS)
		{
			langId = MessageResourceFactory.getLangId(
					getRequest().getLocale().getLanguage(),
					getRequest().getLocale().getCountry()
				);
		}
		else
		{
			langId = user.getLanguage();
		}

		langRes = MessageResourceFactory.getMessageResource(langId);
		getRoot().put(ActionResult.MAP_KEY_I18N, langRes.getResourceBundle());
		getRoot().put(ActionResult.MAP_KEY_LANGUAGE, getLanguages().get(langId));

		int styleId = user.getStyle();
		getRoot().put(ActionResult.MAP_KEY_STYLE, styleId);

		logger.debug("style id:"+styleId+", language: " + getLanguages().get(langId).getFullName());
	}

	/**
	 * 根据 request 中的 session id 和 browser agent 和 IP 信息, 装载用户的SSO session 和 user 等信息
	 * 置入session data map
	 * 
	 * @return
	 */
	protected void loadSession()
	{
		reloadSession(null);
	}

	/**
	 * Load session and user data according to the session id, browser agent (and IP) in request,
	 * and put them into session data map
	 * 
	 * @param newSid: This is for special cases only, e.g. the session id changed during the user login.
	 */
	protected void reloadSession(String newSid)
	{
		Session session;

		if (newSid == null || newSid.length() == 0)
			session = SessionService.getSession(getResponse(), getRequest());
		else
			session = SessionService.getSessionWithSid(getResponse(), getRequest(), newSid);

		User user;
		if (session.getSession_uuid() == SessionConstants.USER_ID_ANONYMOUS)
		{
			user = UserService.getAnonymousUser();
		}
		else
		{
			user = UserService.getUser(session.getSession_uuid());
			// Reset the session UID if the user doesn't exists
			if (user == null)
			{
				user = UserService.getAnonymousUser();
				session.setSession_uuid(SessionConstants.USER_ID_ANONYMOUS);
			}
		}

		getRoot().put(ActionResult.MAP_KEY_SESSION, session);
		logger.debug("Load session, sid:" + session.getSession_id() + " uid:" + session.getSession_uuid() +", ip:"+session.getSession_ip());

		getRoot().put(ActionResult.MAP_KEY_SESSIONUSER, user);
		logger.debug("Load user:" + user.getName() + " uid:" + user.getId());

		if (ACLUtils.accVerify(user.getPermissions(), SimpleConfig.getSuperUserIndexes()))
		{
			getRoot().put(ActionResult.MAP_KEY_SUPERSESSION, 1);
		}
		else
		{
			getRoot().remove(ActionResult.MAP_KEY_SUPERSESSION);
		}
	}

	/**
	 * Get current session
	 * 
	 * @return
	 */
	protected Session getSession()
	{
		return (Session)getRoot().get(ActionResult.MAP_KEY_SESSION);
	}

	/**
	 * Get current session user
	 * 
	 * @return
	 */
	protected User getSessionUser()
	{
		return (User)getRoot().get(ActionResult.MAP_KEY_SESSIONUSER);
	}

	/**
	 * Is current session user a supervisor?
	 * 
	 * @return
	 */
	protected boolean isSuperSession()
	{
		if (getRoot().get(ActionResult.MAP_KEY_SUPERSESSION) == null)
			return false;
		else
			return true;
	}

	protected boolean isLogged()
	{
		User user = getSessionUser();
		if (user.getId() == SessionConstants.USER_ID_ANONYMOUS)
			return false;
		return true;
	}
}
